#include <stdio.h>
#include <string.h>

#include "hi_gpio.h"
#include "hi_spi.h"
#include <hi_io.h>
#include <unistd.h>
#include "iot_gpio.h"

#include "ohos_init.h" 
#include "cmsis_os2.h"

#define RED_INDEX   0
#define GREEN_INDEX 1
#define BLUE_INDEX  2


//功能：把 3BYTE 的RGB数据转换成 24BYTE SPI数据
void WS2812_send(unsigned char *rgb)
{
    unsigned char data[24];
    unsigned char i, bit;
    unsigned char index = 0;
        for(i = 0; i < 8; i++) // GREEN  按位发送
        {
            bit = ((rgb[GREEN_INDEX] << i) & 0x80);
            data[index] = (bit == 0x80) ? 0xfc : 0xe0;//spi发送0xfc 为ws812 1码 spi发送0xe0 为ws812 0码
            index++;
        } 
        for(i = 0; i < 8; i++) // RED
        {
            bit = ((rgb[RED_INDEX]<<i) & 0x80);
            data[index] = (bit==0x80) ? 0xfc : 0xe0;
            index++;
        }
        for(i=0; i<8; i++) // BLUE
        {
            bit = ((rgb[BLUE_INDEX]<<i) & 0x80);
            data[index] = (bit==0x80) ? 0xfc : 0xe0;
            index++;
        }
        hi_spi_host_write(HI_SPI_ID_0, data, 24);
}

 

void WS2812b(void)//PARTY
{

    //色带
    unsigned char color[8][3]=
    {
        {255,255,255},//白
        // {255,255, 0 },//黄
        {255,255,255},
        {255,255,255},
        {255,255,255},
        {255,255,255},
        {255,255,255},
        {255,255,255},

    };//改变灯光颜色
 
    //初始化GPIO
    IoTGpioInit(HI_IO_NAME_GPIO_12);
    IoTGpioInit(HI_IO_NAME_GPIO_10);
    IoTGpioInit(HI_IO_NAME_GPIO_9);

    hi_spi_deinit(HI_SPI_ID_0); /* if wake_up from deep sleep, should deinit first */
    hi_spi_cfg_basic_info spi_cfg_basic_info;
    spi_cfg_basic_info.cpha = 0;
    spi_cfg_basic_info.cpol = 0;
    spi_cfg_basic_info.data_width = HI_SPI_CFG_DATA_WIDTH_E_8BIT;
    spi_cfg_basic_info.endian = 0;
    spi_cfg_basic_info.fram_mode = 0;
    spi_cfg_basic_info.freq = 8*900000; 
    hi_spi_cfg_init_param spi_init_param = {0};
    spi_init_param.is_slave = 0;
    hi_spi_init(HI_SPI_ID_0, spi_init_param, &spi_cfg_basic_info); //基本参数配置
    hi_spi_set_loop_back_mode(HI_SPI_ID_0, 1);
    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_SPI0_CSN);//io配置
    hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_SPI0_CK);
    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_SPI0_TXD);
    hi_io_set_driver_strength(HI_IO_NAME_GPIO_6, HI_IO_DRIVER_STRENGTH_2);//驱动能力
    hi_spi_set_irq_mode(HI_SPI_ID_0, 0);
    hi_spi_set_dma_mode(HI_SPI_ID_0, 1);
 
    unsigned char data[50];
    unsigned char rgb[3];
    unsigned int i;
    while (1)
    {
        i=0;
        for (i = 0; i < 256; i ++)
        {
            memset(data, 0, sizeof(data));
            hi_spi_host_write(HI_SPI_ID_0, data, sizeof(data));//前50byte为低电平break
 
            for (unsigned int j = 0; j < 256; j ++)//亮度
            {
                rgb[0] = color[i%7][0]&j;//色带
                rgb[1] = color[i%7][1]&j;
                rgb[2] = color[i%7][2]&j;
                for (unsigned int k = 0; k < 256; k ++)//控制256个灯
                {
                    WS2812_send(rgb);
                }
                usleep(10 * 1000);
            }
            
        }
    }
}


// 开灯函数，设置LED为白光
void turnOnLight() 
{
    IoTGpioInit(HI_IO_NAME_GPIO_12);
    IoTGpioInit(HI_IO_NAME_GPIO_10);
    IoTGpioInit(HI_IO_NAME_GPIO_9);

    hi_spi_deinit(HI_SPI_ID_0); /* if wake_up from deep sleep, should deinit first */
    hi_spi_cfg_basic_info spi_cfg_basic_info;
    spi_cfg_basic_info.cpha = 0;
    spi_cfg_basic_info.cpol = 0;
    spi_cfg_basic_info.data_width = HI_SPI_CFG_DATA_WIDTH_E_8BIT;
    spi_cfg_basic_info.endian = 0;
    spi_cfg_basic_info.fram_mode = 0;
    spi_cfg_basic_info.freq = 8*900000; 
    hi_spi_cfg_init_param spi_init_param = {0};
    spi_init_param.is_slave = 0;
    hi_spi_init(HI_SPI_ID_0, spi_init_param, &spi_cfg_basic_info); //基本参数配置
    hi_spi_set_loop_back_mode(HI_SPI_ID_0, 1);
    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_SPI0_CSN);//io配置
    hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_SPI0_CK);
    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_SPI0_TXD);
    hi_io_set_driver_strength(HI_IO_NAME_GPIO_6, HI_IO_DRIVER_STRENGTH_2);//驱动能力
    hi_spi_set_irq_mode(HI_SPI_ID_0, 0);
    hi_spi_set_dma_mode(HI_SPI_ID_0, 1);
 
    unsigned char rgb[3] = {255, 255, 255}; // 白光RGB值
    unsigned char data[24]; // WS2812B需要24字节数据

    for (int i = 0; i < 24; i += 3) {
        data[i] = rgb[0];
        data[i + 1] = rgb[1];
        data[i + 2] = rgb[2];
    }

    for (unsigned int k = 0; k < 256; k++) {
        WS2812_send(data); // 发送白光数据
    }
}


// 点亮弱黄光函数，设置LED为弱黄光
void YellowLight() 
{
    IoTGpioInit(HI_IO_NAME_GPIO_12);
    IoTGpioInit(HI_IO_NAME_GPIO_10);
    IoTGpioInit(HI_IO_NAME_GPIO_9);

    hi_spi_deinit(HI_SPI_ID_0); /* if wake_up from deep sleep, should deinit first */
    hi_spi_cfg_basic_info spi_cfg_basic_info;
    spi_cfg_basic_info.cpha = 0;
    spi_cfg_basic_info.cpol = 0;
    spi_cfg_basic_info.data_width = HI_SPI_CFG_DATA_WIDTH_E_8BIT;
    spi_cfg_basic_info.endian = 0;
    spi_cfg_basic_info.fram_mode = 0;
    spi_cfg_basic_info.freq = 8*900000; 
    hi_spi_cfg_init_param spi_init_param = {0};
    spi_init_param.is_slave = 0;
    hi_spi_init(HI_SPI_ID_0, spi_init_param, &spi_cfg_basic_info); //基本参数配置
    hi_spi_set_loop_back_mode(HI_SPI_ID_0, 1);
    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_SPI0_CSN);//io配置
    hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_SPI0_CK);
    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_SPI0_TXD);
    hi_io_set_driver_strength(HI_IO_NAME_GPIO_6, HI_IO_DRIVER_STRENGTH_2);//驱动能力
    hi_spi_set_irq_mode(HI_SPI_ID_0, 0);
    hi_spi_set_dma_mode(HI_SPI_ID_0, 1);
 
    unsigned char rgb[3] = {127, 100, 0}; // 弱黄光RGB值
    unsigned char data[24]; // WS2812B需要24字节数据

    for (int i = 0; i < 24; i += 3) {
        data[i] = rgb[0];
        data[i + 1] = rgb[1];
        data[i + 2] = rgb[2];
    }

    for (unsigned int k = 0; k < 256; k++) {
        WS2812_send(data); // 发送弱黄光数据
    }
}


// 关灯函数，关闭LED
void turnOffLight() 
{

    IoTGpioInit(HI_IO_NAME_GPIO_12);
    IoTGpioInit(HI_IO_NAME_GPIO_10);
    IoTGpioInit(HI_IO_NAME_GPIO_9);

    hi_spi_deinit(HI_SPI_ID_0);/* if wake_up from deep sleep, should deinit first */
    hi_spi_cfg_basic_info spi_cfg_basic_info;
    spi_cfg_basic_info.cpha = 0;
    spi_cfg_basic_info.cpol = 0;
    spi_cfg_basic_info.data_width = HI_SPI_CFG_DATA_WIDTH_E_8BIT;
    spi_cfg_basic_info.endian = 0;
    spi_cfg_basic_info.fram_mode = 0;
    spi_cfg_basic_info.freq = 8*900000; 
    hi_spi_cfg_init_param spi_init_param = {0};
    spi_init_param.is_slave = 0;
    hi_spi_init(HI_SPI_ID_0, spi_init_param, &spi_cfg_basic_info); //基本参数配置
    hi_spi_set_loop_back_mode(HI_SPI_ID_0, 1);
    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_SPI0_CSN);//io配置
    hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_SPI0_CK);
    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_SPI0_TXD);
    hi_io_set_driver_strength(HI_IO_NAME_GPIO_6, HI_IO_DRIVER_STRENGTH_2);//驱动能力
    hi_spi_set_irq_mode(HI_SPI_ID_0, 0);
    hi_spi_set_dma_mode(HI_SPI_ID_0, 1);
 
    unsigned char data[24]; // 关闭LED需要发送全0数据

    memset(data, 0, sizeof(data)); // 将数据置为0

    for (unsigned int k = 0; k < 256; k++) {
        WS2812_send(data); // 发送全0数据关闭LED
    }
}

